﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pipes.Data.Table
{
  public static class Extension
  {
    public static ITable SelectColumns(this ITable table, Func<IColumnMetadata, bool> criteria)
    {
      var queryable = table as IQueryableTable;
      if (queryable == null) queryable = new QueryableTable(table);
      queryable.AddColumnFilter(criteria);
      return queryable;
    }
    public static ITable Where(this ITable table, Func<IDataRecord, bool> criteria)
    {
      var queryable = table as IQueryableTable;
      if (queryable == null) queryable = new QueryableTable(table);
      queryable.AddRowFilter(criteria);
      return queryable;
    }
    public static void WriteTo(this ITable table, ITableWriter writer)
    {
      writer.Head();
      foreach (var col in table.Columns)
      {
        writer.Column(col);
      }
      writer.HeadEnd();
      foreach (var row in table)
      {
        writer.Row();
        foreach (var cell in row)
        {
          writer.Cell(cell);
        }
        writer.RowEnd();
      }
      writer.Flush();
    }
  }
}
